package com.dfhc.pub.verificationcode;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.ImageFilter;

import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomRangeColorGenerator;
import com.octo.captcha.component.image.color.SingleColorGenerator;
import com.octo.captcha.component.image.deformation.ImageDeformation;
import com.octo.captcha.component.image.deformation.ImageDeformationByFilters;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.textpaster.textdecorator.LineTextDecorator;
import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
import com.octo.captcha.component.image.wordtoimage.DeformedComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.FileDictionary;
import com.octo.captcha.component.word.wordgenerator.ComposeDictionaryWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;

/**
 * 仿照JCaptcha2.0编写GMail验证码样式的图片引擎.
 * 
 * @author calvin
 */
public class GMailEngine extends ListImageCaptchaEngine {
	@Override
	protected void buildInitialFactories() {
		int minWordLength = 4;
		int maxWordLength = 5;
		int fontSize = 65;
		int imageWidth = 250;
		int imageHeight = 100;

		//word generator
		WordGenerator dictionnaryWords = new ComposeDictionaryWordGenerator(new FileDictionary("toddlist"));
		// 背景颜色随机生成  
		// 验证码的颜色-使用随机颜色器new Integer[]{0,100},new Integer[]{0,100}, new  
		// Integer[]{0,100}  
		RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(  
		new int[] { 0, 255 }, new int[] { 0, 255 },  
		new int[] { 0, 255 });
//		BackgroundGenerator backgroundGenerator = new GradientBackgroundGenerator(  
//				imageWidth, imageHeight, Color.white, Color.white);
//		backgroundGenerator = new FunkyBackgroundGenerator(imageWidth, imageHeight); 
//		BaffleTextDecorator baffleTextDecorator = new BaffleTextDecorator(2,cgen);//气泡干扰  
//		LineTextDecorator lineTextDecorator = new LineTextDecorator(1,cgen);//曲线干扰  
		//word2image components
//		TextPaster randomPaster = new DecoratedRandomTextPaster(minWordLength, maxWordLength,
//				new RandomListColorGenerator(new Color[] { new Color(23, 170, 27), new Color(220, 34, 11),
//						new Color(23, 67, 172) }), new TextDecorator[] {new LineTextDecorator(new Integer(1), Color.GRAY) });
		TextPaster randomPaster = new DecoratedRandomTextPaster(minWordLength, maxWordLength,
				cgen, new TextDecorator[] {new LineTextDecorator(new Integer(1), Color.GRAY) });
		//BackgroundGenerator background = new UniColorBackgroundGenerator(imageWidth, imageHeight, Color.white);
		BackgroundGenerator background =  new GradientBackgroundGenerator(imageWidth, imageHeight,
				new SingleColorGenerator(new Color(235, 255, 255)), new SingleColorGenerator(new Color(255, 195, 230)));
		FontGenerator font = new RandomFontGenerator(fontSize, fontSize, new Font[] {
				new Font("nyala", Font.BOLD|Font.ITALIC|Font.CENTER_BASELINE, fontSize), new Font("Bell MT", Font.PLAIN, fontSize),
				new Font("Credit valley", Font.BOLD|Font.ITALIC|Font.CENTER_BASELINE, fontSize) });

		ImageDeformation postDef = new ImageDeformationByFilters(new ImageFilter[] {});
		ImageDeformation backDef = new ImageDeformationByFilters(new ImageFilter[] {});
		ImageDeformation textDef = new ImageDeformationByFilters(new ImageFilter[] {});

		WordToImage word2image = new DeformedComposedWordToImage(font, background, randomPaster, backDef, textDef,
				postDef);
		addFactory(new GimpyFactory(dictionnaryWords, word2image));
	}

}
